package com.intelligentmeterreadingsys.otherequipment.activity;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.httpserver.RequestUrl;
import com.intelligentmeterreadingsys.httpserver.UploadHttpClient;
import com.intelligentmeterreadingsys.otherequipment.db.EquipementDao;
import com.intelligentmeterreadingsys.povo.OtherEquipmentPOVO;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 设备信息修改上传
 * @author king
 *
 */
public class EquipementModifyUploadActivity extends Activity{
 public Button button;//上传按钮
	 
	 public TextView textview1;//需上传总数
	 
	 public TextView textview2;//未上传总数
	 
	 public TextView textview3;//进度条描述
	 
	 public TextView textview4;
	 
	 public ProgressBar pb;//进度条
	 
	 public List<OtherEquipmentPOVO> list=null;
	 public String url;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.waterbanding_onetoupload);
		init();
	}

	/**
	 * 初始化控件
	 */
	public void init(){
		 RequestUrl r=new RequestUrl(this);
		   url=r.getUrl(RequestUrl.EQUIPEMENTMODIFY);
		   button=(Button)findViewById(R.id.waterbanding_upload_button);
		   pb=(ProgressBar)findViewById(R.id.waterbanding_progressBar);
		   textview1=(TextView)findViewById(R.id.waterbanding_upload_total);
		   textview2=(TextView)findViewById(R.id.waterbanding_noupload_total);
		   textview3=(TextView)findViewById(R.id.waterbanding_pb_textview);
		   textview4=(TextView)findViewById(R.id.onekeyupload_info);
		   textview4.setText("设备修改一键上传");
		   EquipementDao dao=new EquipementDao(this);
		   final int noupload=dao.queryNoUploadTotal("修改");
		   textview1.setText(dao.queryUploadTotal("修改")+"");
		   textview2.setText(noupload+"");
		   textview3.setText("0%");
		   list=dao.queryUploadEquipment(null, "修改", "0", null);
		   dao.close();
		   
		   button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(noupload<1){
					Toast.makeText(EquipementModifyUploadActivity.this, "没有可上传的数据", Toast.LENGTH_SHORT).show();
					return;
				}
				textview3.setText("正在上传中....");
				pb.setMax(noupload);
				myHandler.postDelayed(myRunnable, 500);				
			}
		});
		   
	}
	
	/**
	 * 上传消息处理
	 */
	Handler myHandler =new Handler(){

		@Override
		public void handleMessage(Message msg) {
		   if(msg.what==0){
			   textview3.setText("0%");
			   Toast.makeText(EquipementModifyUploadActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
			   myHandler.removeCallbacks(myRunnable);
		   }else if(msg.what==1){
			   EquipementDao dao=new EquipementDao(EquipementModifyUploadActivity.this);
			   dao.modifyStatus(list.get(msg.arg1).getId(), "修改");
			   dao.close();
			   pb.setProgress(msg.arg1+1);
			   textview3.setText("正在上中.. "+(float)(msg.arg1+1)/list.size());
			   if((msg.arg1+1)==list.size()){
				   Toast.makeText(EquipementModifyUploadActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
				   myHandler.removeCallbacks(myRunnable);
			   }
		   }
			
			super.handleMessage(msg);
		}
		
	};
	
	/**
	 * 上传线程
	 */
	Runnable myRunnable=new Runnable() {
		
		@Override
		public void run() {
			
			for(int i=0;i<list.size();i++){
				Map<String, String> map1=new HashMap<String, String>();
				Map<String, String> map2=new HashMap<String, String>();
				Map<File, String> map3=new HashMap<File, String>();
				OtherEquipmentPOVO e=list.get(i);
				map1.put("number", e.getNumber());
				map1.put("typeId", e.getTypeid());
				map1.put("date", e.getDate());
				map1.put("addr", e.getAdds());
				map1.put("xxAddr", e.getLocationadds());
				map1.put("rfid", e.getRfidNumber());
				map1.put("jingdu", e.getLongitude()+"");
				map1.put("weidu", e.getLatitude()+"");
				map1.put("name", e.getDesc());
				for(int k=0;k<e.getList().size();k++){
					File f=new File(e.getList().get(k));
					if(f.exists()){
						map2.put(f.getName(), "imagename");
						map3.put(f, "imagepath");
					}
				}
			 String respone= UploadHttpClient.uploadSubmit(url, map1, map3, map2);
			 if(respone.equals("error")){
				 Message msg=myHandler.obtainMessage();
				 msg.what=0;
				 myHandler.sendMessage(msg);
			 }else{
				 try {
					JSONObject jb=new JSONObject(respone);
					String message=jb.getString("message");
					if(message.equals("yes")){
						 Message msg=myHandler.obtainMessage();
						 msg.what=1;
						 msg.arg1=i;
						 myHandler.sendMessage(msg);
					}else{
						 Message msg=myHandler.obtainMessage();
						 msg.what=0;
						 myHandler.sendMessage(msg);
					}
				} catch (JSONException e1) {
					
					 Message msg=myHandler.obtainMessage();
					 msg.what=0;
					 myHandler.sendMessage(msg);
				}
			 }
			}
			
		}
	};
}
